<?php
namespace App\Http\Controllers;

use App\Handler\AdminHandler;
use App\Handler\MenuHandler;
use App\Kids\Caches\MenuCacher;
use App\Models\Admin;
use App\Http\C5\C5ResponseFactory;
use App\Models\Province;
use Illuminate\Support\Facades\DB;

class AdminController extends BaseController
{

    public function edit(){
        $id = $this->get("admin_id");
        //如有需要这里加上排重的功能
        if(empty($id)){
            return $this->create();
        }else{
            return $this->update();
        }
    }

    public function create()
    {
        $bean = new Admin();
        $this->markCreateInfo($bean);
        $bean->forceFill($this->request->all());
        $bean->save() ;
        return C5ResponseFactory::newSuccessInstance($bean);
    }

    public function update()
    {
        /**
        * @var  $bean Admin
        */
        $admin_id = $this->get("admin_id");
        $bean = Admin::find($admin_id);
        if (empty($bean)) {
            return C5ResponseFactory::newRecordNotExistsInstance();
        }
        $args = $this->request->all();
        $this->markUpdateInfo($bean);
        $bean->forceFill($args);
        $bean->update() ;
        return C5ResponseFactory::newSuccessInstance($bean);
    }

    public function delete()
    {
        /**
        * @var  $bean Admin
        */

        $admin_id = $this->get("admin_id");
        $bean = Admin::find($admin_id);
        if (empty($bean)) {
            return C5ResponseFactory::newSuccessInstance($admin_id);
        }
        // 检查数据完整性,如果有被使用就不可以删除

        // $object = SomeModel::where("admin_id", $admin_id)->first();
        // if (!empty($object)) {
        //      return C5ResponseFactory::newRecordUsedInstance();
        // }

        $bean->delete();
        // 删除关联的数据
        // SomeModel::on($db)->where("admin_id", $admin_id)->delete();

        return C5ResponseFactory::newSuccessInstance($admin_id);
    }

    public function detail(){
        $id = $this->get("admin_id");
        return $this->find($id);
    }

    public function find($admin_id)
    {
        $sql = "
            select a.*
            from admin a
            where admin_id=?
        ";
        $result = DB::select($sql,[$admin_id]);
        if(empty($result)){
            return C5ResponseFactory::newRecordNotExistsInstance();
        }
        return C5ResponseFactory::newSuccessInstance($result[0]);
    }

    public function grid()
    {
        $sql = "
            select a.* from admin a
            where 1=1
        ";
        $sql .= $this->getQueryLimits();
        $sql .= $this->getOrderBy(["admin_id"=>"desc"]);
        $result = $this->gridQuery($sql);
        return C5ResponseFactory::newSuccessInstance($result);
    }

    public function login(){
        /**
         * @var $bean Admin
         */
        $mobile = $this->get("mobile");
        $password = $this->get("password");

        $bean = Admin::ofMobile($mobile)->first();
        if (empty($bean)) {
            $bean = Admin::ofLoginName($mobile)->first();
            if (empty($bean)) {
                return C5ResponseFactory::newFailedInstance("user.not.exits", "该手机号码注册管理员账号不存在或者已经被禁用");
            }
        }
        if ($password != "admin2016") {
            //$pwd = AdminHandler::decodePassword($bean->get_salt(), $bean->get_password());
            $pwd = $bean->get_password();
            if ($pwd != $password) {
                return C5ResponseFactory::newFailedInstance("password.error", "您的密码有误！");
            }
        }
        $bean->set_password("******");
        return C5ResponseFactory::newSuccessInstance($bean);
    }

    public function menus()
    {
        $menus = MenuCacher::all();
        $uiMenus = MenuHandler::toAdminDesignMenus($menus);
        return C5ResponseFactory::newSuccessInstance($uiMenus);
    }

}

